package com.ruoyi.demo04;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * @Author Husp
 * @Date 2023/12/10 16:09
 */
public class Test04 {

    public static void main(String[] args) throws InterruptedException {
        //1.定义集合存储200个红包
        List<Integer> moneys = getMoney();
        //1.1 红包打乱
        Collections.shuffle(moneys);
        //2.创建100员工线程数
        List<EmployeeThread> employeeThreads = new ArrayList<>(100);
        for (int i = 1; i <= 100; i++) {
            EmployeeThread employeeThread = new EmployeeThread(moneys, i + "号");
            employeeThreads.add(employeeThread);
            //启动线程
            employeeThread.start();
        }
        //3.统计抢到红包数最多的线程 join()，按线程红包总价差价降序排序
        for (EmployeeThread employeeThread : employeeThreads) {
            employeeThread.join();
        }
        System.out.println("红包已经抢完了！");

        employeeThreads.stream().sorted((thread1, thread2) ->
            thread2.getTotalMoney() - thread1.getTotalMoney()
        ).forEach(thread -> {
            System.out.println(thread.getName() + "总共抢到了" + thread.getTotalMoney() + "元");
        });

    }

    private static List<Integer> getMoney() {
        //1.1 创建集合存红包金额，200个红包100元
        List<Integer> moneys = new ArrayList<>(100);
        //1.2 给红包分配权重
        Random r = new Random();
        //1.3 [1-30]的小红包占比(比例：80%)个数
        for (int i = 1; i <= 160; i++) {
            moneys.add(r.nextInt(30) + 1);
        }
        //1.4 [30-100]的大红包占比(比例：20%)个数
        for (int i = 1; i <= 40; i++) {
            moneys.add(r.nextInt(70) + 31);
        }
        return moneys;
    }
}
